<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
			<el-form-item label="题号" prop="questionId">
				<el-input v-model="queryParams.questionId" placeholder="请输入题号" clearable size="small" @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="题目类别" prop="questionType">
				<el-select v-model="queryParams.questionType" placeholder="请选择题目类别" clearable size="small">
					<el-option v-for="dict in questionTypeOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>
		<el-row :gutter="10" class="mb8">
			<div v-if="testpaper == 1">
				<el-col :span="1.5">
					<el-button type="warning" plain icon="el-icon-check" size="mini" :disabled="multiple" @click="confirmuse">确认添加题目到试卷</el-button>
				</el-col>
			</div>
			<div v-if="examination == 0 && testpaper == 0">
				<el-col :span="1.5">
					<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:examquestionbank:add']">添加题目</el-button>
				</el-col>
				<el-col :span="1.5">
					<el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
					 v-hasPermi="['system:examquestionbank:remove']">删除题目</el-button>
				</el-col>
				</div>
				<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
				
		</el-row>
		<el-table v-loading="loading" :data="examinationquestionList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="题号" align="center" prop="questionId" />
			<el-table-column label="题目类别" align="center" prop="questionType" />
			<el-table-column label="题目内容" align="center" prop="questionContent" />
			<el-table-column label="答案类型" align="center" prop="questionAnswerType" />
			<el-table-column label="设置时间" align="center" prop="setDate" />
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width" v-if="examination == 0 && testpaper == 0">
				<template slot-scope="scope">
					<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:examquestionbank:edit']">编辑题目</el-button>
					<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['system:examquestionbank:remove']">删除题目</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
		 @pagination="getList" />

		<!-- 添加或修改题目库对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="题目类别" prop="questionType">
					<el-select v-model="form.questionType" placeholder="请选择题目类别">
						<el-option v-for="dict in questionTypeOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
					</el-select>
				</el-form-item>
				<el-form-item label="题目内容">
					<!-- <editor v-model="form.questionContent" :min-height="192"/> -->
					<el-input v-model="form.questionContent" type="textarea" :rows="2" placeholder="请输入题目内容" />
				</el-form-item>
				<div v-if="form.questionAnswerType == 0">
					<el-form-item label="题目选项A" prop="selectA">
						<el-input v-model="form.selectA" placeholder="请输入题目选项A" />
					</el-form-item>
					<el-form-item label="题目选项B" prop="selectB">
						<el-input v-model="form.selectB" placeholder="请输入题目选项B" />
					</el-form-item>
					<el-form-item label="题目选项C" prop="selectC">
						<el-input v-model="form.selectC" placeholder="请输入题目选项C" />
					</el-form-item>
					<el-form-item label="题目选项D" prop="selectD">
						<el-input v-model="form.selectD" placeholder="请输入题目选项D" />
					</el-form-item>
				</div>
				<el-form-item label="题目答案" prop="questionAnswer">
					<div v-if="form.questionAnswerType == 0">
						<el-select v-model="form.questionAnswer" placeholder="请选择题目答案">
							<el-option v-for="dict in questionAnswerOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
						</el-select>
					</div>
					<div v-else>
						<el-select v-model="form.questionAnswer" placeholder="请选择题目答案">
							<el-option v-for="dict in questionanswerOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
						</el-select>
					</div>
				</el-form-item>
				<el-form-item label="答案类型" prop="questionAnswerType">
					<el-select v-model="form.questionAnswerType" placeholder="请选择答案类型">
						<el-option v-for="dict in questionAnswerTypeOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
					</el-select>
				</el-form-item>
				<el-form-item label="设置人员" prop="setId">
					<el-input v-model="form.setId" placeholder="请输入设置人员" />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		listExaminationquestion,
		getExaminationquestion,
		delExaminationquestion,
		addExaminationquestion,
		updateExaminationquestion,
		exportExaminationquestion,
		confirmSelection
	} from "@/api/system/examinationquestion";
	import Editor from '@/components/Editor';

	export default {
		name: "Examinationquestion",
		components: {
			Editor,
		},
		data() {
			return {
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				//查看是否是从安全考试跳转进入
				examination: 0,
				//查看是否是从安全试卷跳转进去
				testpaper: 0,
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 题目库表格数据
				examinationquestionList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				//题目类别字典
				questionTypeOptions: [],
				//确认选择题目到该试卷的参数
				papersselectParams: {
					question: [],
					examinationPaperId: null,
					setId: null
				},
				//答案类别字典
				questionAnswerTypeOptions: [],
				//题目答案类别字典
				questionAnswerOptions: [],
				questionanswerOptions: [],
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					questionType: null,
					questionContent: null,
					selectA: null,
					selectB: null,
					selectC: null,
					selectD: null,
					questionAnswer: null,
					questionAnswerType: null,
					setId: null,
					setDate: null
				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {
					questionType: [{
						required: true,
						message: "题目类别不能为空",
						trigger: "change"
					}],
					selectA: [{
						required: true,
						message: "题目选项A不能为空",
						trigger: "blur"
					}],
					selectB: [{
						required: true,
						message: "题目选项B不能为空",
						trigger: "blur"
					}],
					selectC: [{
						required: true,
						message: "题目选项C不能为空",
						trigger: "blur"
					}],
					selectD: [{
						required: true,
						message: "题目选项D不能为空",
						trigger: "blur"
					}],
					questionAnswer: [{
						required: true,
						message: "题目答案不能为空",
						trigger: "blur"
					}],
					questionAnswerType: [{
						required: true,
						message: "答案类型不能为空",
						trigger: "change"
					}],
					setId: [{
						required: true,
						message: "设置人员不能为空",
						trigger: "blur"
					}],
					setDate: [{
						required: true,
						message: "设置时间不能为空",
						trigger: "blur"
					}]
				}
			};
		},
		created() {
			this.getList();
			this.getDicts("question_type").then(response => {
				this.questionTypeOptions = response.data;
			})
			this.getDicts("question_answer_type").then(response => {
				this.questionAnswerTypeOptions = response.data;
			})
			this.getDicts("select_answer").then(response => {
				this.questionAnswerOptions = response.data;
			})
			this.getDicts("boolean_anwser").then(response => {
				this.questionanswerOptions = response.data;
			})
		},
		methods: {
			/** 查询题目库列表 */
			getList() {
				this.loading = true;
				if (this.$route.query.memo == 2) {
					if (this.$route.query.examinationId != undefined) {
						this.examination = 1;
					}
					this.testpaper = 2;
					this.queryParams.memo = this.$route.query.memo;
					this.queryParams.safetyExaminationPaperId = this.$route.query.safetyExaminationPaperId;
				} else if (this.$route.query.memo == 1) {
					if (this.$route.query.examinationId != undefined) {
						this.examination = 1;
					}
					this.testpaper = 1;
					this.queryParams.memo = this.$route.query.memo;
					this.queryParams.safetyExaminationPaperId = this.$route.query.safetyExaminationPaperId;
				} else {
					this.examination = 0;
					this.testpaper = 0;
					this.queryParams.memo = 0;
				}
				listExaminationquestion(this.queryParams).then(response => {
					this.examinationquestionList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					questionId: null,
					questionType: null,
					questionContent: null,
					selectA: null,
					selectB: null,
					selectC: null,
					selectD: null,
					questionAnswer: null,
					questionAnswerType: null,
					setId: null,
					setDate: null
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.questionId)
				this.single = selection.length !== 1
				this.multiple = !selection.length
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.open = true;
				this.title = "添加题目库";
				this.form.setId = this.$store.getters.userId;
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				const questionId = row.questionId || this.ids
				getExaminationquestion(questionId).then(response => {
					this.form = response.data;
					this.form.setId = this.$store.getters.userId;
					this.open = true;
					this.title = "修改题目库";
				});
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						if (this.form.questionId != null) {
							updateExaminationquestion(this.form).then(response => {
								this.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							});
						} else {
							addExaminationquestion(this.form).then(response => {
								this.msgSuccess("新增成功");
								this.open = false;
								this.getList();
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const questionIds = row.questionId || this.ids;
				this.$confirm('是否确认删除题目库编号为"' + questionIds + '"的数据项?', "警告", {
					confirmButtonText: "确定",
					cancelButtonText: "取消",
					type: "warning"
				}).then(function() {
					return delExaminationquestion(questionIds);
				}).then(() => {
					this.getList();
					this.msgSuccess("删除成功");
				})
			},
			/** 确认选择题目到该试卷 */
			confirmuse(row) {
				var that = this;
				that.papersselectParams.question = [];
				const questionIds = row.questionId || this.ids;
				this.$confirm('是否确认使用试卷编号为"' + questionIds + '"的试卷?', "提示", {
					confirmButtonText: "确定",
					cancelButtonText: "取消",
					type: "success"
				}).then(function() {
					for (let val of questionIds) {
						that.papersselectParams.question.push({
							'questionId': val
						});
					}
					that.papersselectParams.examinationPaperId = that.$route.query.safetyExaminationPaperId;
					that.papersselectParams.setId = that.$store.getters.userId;
					return confirmSelection(that.papersselectParams);
				}).then(() => {
					this.getList();
					this.msgSuccess("使用成功");
					this.$router.push({
						path: "/safetyexam/testpaperinformation"
					});
				})
			}
		}
	};
</script>
